﻿using Apps.Common;
using Apps.Models;
using System.Linq;
using System.Collections.Generic;
using Apps.Models.PUB;

namespace Apps.BLL.PUB
{
    public partial class PUB_CY_STOCKPILEBLL
    {

        public override List<PUB_CY_STOCKPILEModel> GetListByParentId(ref GridPager pager, string queryStr, object parentId)
        {
            IQueryable<PUB_CY_STOCKPILE> queryData = null;
            string pid = parentId.ToString();
            if (pid != "0")
            {
                queryData = m_Rep.GetList(a => a.PUB_CY_PARTITION.PARTITION_ID == pid);
            }
            else
            {
                queryData = m_Rep.GetList();
            }
            if (!string.IsNullOrWhiteSpace(queryStr))
            {
                queryData = m_Rep.GetList(
                            a => (
                                    a.STOCKPILE_ID.Contains(queryStr)
                                   || a.STOCKPILE_NAME.Contains(queryStr)
                                   || a.PARTITION_ID.Contains(queryStr)
                                 )
                            );
            }
            pager.totalRows = queryData.Count();
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }
        public override List<PUB_CY_STOCKPILEModel> CreateModelList(ref IQueryable<PUB_CY_STOCKPILE> queryData)
        {

            List<PUB_CY_STOCKPILEModel> modelList = (from r in queryData
                                                     select new PUB_CY_STOCKPILEModel
                                                     {
                                                         STOCKPILE_ID = r.STOCKPILE_ID,
                                                         STOCKPILE_NAME = r.STOCKPILE_NAME,
                                                         PARTITION_ID = r.PARTITION_ID,
                                                         BEGIN_LENGTH = r.BEGIN_LENGTH,
                                                         END_LENGTH = r.END_LENGTH,
                                                         BEGIN_WIDTH = r.BEGIN_WIDTH,
                                                         END_WIDTH = r.END_WIDTH,
                                                         BEGIN_HEIGHT = r.BEGIN_HEIGHT,
                                                         END_HEIGHT = r.END_HEIGHT,
                                                         CY_PARTITIONName = r.PUB_CY_PARTITION.PARTITION_NAME,
                                                     }).ToList();
            return modelList;
        }
        public override PUB_CY_STOCKPILEModel GetById(object id)
        {
            if (IsExists(id))
            {

                PUB_CY_STOCKPILE entity = m_Rep.GetById(id);
                PUB_CY_STOCKPILEModel model = new PUB_CY_STOCKPILEModel();
                model.STOCKPILE_ID = entity.STOCKPILE_ID;
                model.STOCKPILE_NAME = entity.STOCKPILE_NAME;
                model.PARTITION_ID = entity.PARTITION_ID;
                model.BEGIN_LENGTH = entity.BEGIN_LENGTH;
                model.END_LENGTH = entity.END_LENGTH;
                model.BEGIN_WIDTH = entity.BEGIN_WIDTH;
                model.END_WIDTH = entity.END_WIDTH;
                model.BEGIN_HEIGHT = entity.BEGIN_HEIGHT;
                model.END_HEIGHT = entity.END_HEIGHT;
                model.CY_PARTITIONName = entity.PUB_CY_PARTITION.PARTITION_NAME;
                return model;
            }
            else
            {
                return null;
            }
        }
    }
}

